//grab racial moments from data

import delimited using "$data/NLSY97/region/region.csv", clear
ren *, upper
do "$data/NLSY97/region/region-value-labels.do"
ren *, lower
ren pubid uniqid
ren keyrace race
drop key*
drop cv_sample
ren cv region
save "$temp/nlsy_1997_regions", replace


//earnings
//college by region
set seed 1234
use "$temp/nlsy_moment_data", clear
merge 1:1 uniqid using "$temp/nlsy_1997_regions", keep(match) nogen
drop if hgc_final>20
gen coll_final = (hgc_final>=16)
su coll_final [fw=weight_17]

su coll_final [fw=weight_17] if region == 1
local coll_region_1 = `r(mean)'
su coll_final [fw=weight_17] if region == 2
local coll_region_2 = `r(mean)'
su coll_final [fw=weight_17] if region == 3
local coll_region_3 = `r(mean)'
su coll_final [fw=weight_17] if region == 4
local coll_region_4 = `r(mean)'





//college
use "$temp/nlsy_moment_data", clear
merge 1:1 uniqid using "$temp/nlsy_1997_regions", keep(match) nogen
drop if hgc_final>20
gen coll_final = (hgc_final>=16)
su coll_final [fw=weight_17]

su coll_final [fw=weight_17] if race == 1
local collb = `r(mean)'
su coll_final [fw=weight_17] if race == 2
local collh = `r(mean)'


qui{
forval i = 1/1000{
	use "$temp/nlsy_moment_data", clear
	merge 1:1 uniqid using "$temp/nlsy_1997_regions", keep(match) nogen
	bsample
	drop if hgc_final>20
	gen coll_final = (hgc_final>=16)
	su coll_final [fw=weight_17]

	su coll_final [fw=weight_17] if race == 1
	local collb_boot = `r(mean)'
	su coll_final [fw=weight_17] if race == 2
	local collh_boot = `r(mean)'
	
	
	su coll_final [fw=weight_17] if region == 1
	local coll_region_1_boot = `r(mean)'
	su coll_final [fw=weight_17] if region == 2
	local coll_region_2_boot = `r(mean)'
	su coll_final [fw=weight_17] if region == 3
	local coll_region_3_boot = `r(mean)'
	su coll_final [fw=weight_17] if region == 4
	local coll_region_4_boot = `r(mean)'
		
	clear
	set obs 6
	gen moment = .
	replace moment = `collb_boot' in 1
	replace moment = `collh_boot' in 2
	replace moment = `coll_region_1_boot' in 3
	replace moment = `coll_region_2_boot' in 4
	replace moment = `coll_region_3_boot' in 5
	replace moment = `coll_region_4_boot' in 6
	xpose, clear
	save "$temp/boot/coll_moments_race_boot_`i'", replace		
}
}

clear
forval i = 1/1000{
    append using "$temp/boot/coll_moments_race_boot_`i'"
}
export delimited "$dir/Model/boot/coll_race_boot_sample.csv", novarn replace


//migration and income
use "$temp/acs_master_kid_sample", clear
su move [fw=perwt] if racetype == 2
local migb = `r(mean)'
su move [fw=perwt] if racetype == 3
local migh = `r(mean)'
su incwage_orig [fw=perwt] if racetype == 2
local incb = `r(mean)'
su incwage_orig [fw=perwt] if racetype == 3
local inch = `r(mean)'
su incwage_orig [fw=perwt] if racetype == 1
local incw = `r(mean)'

local frac_b = `incb'/`incw'
local frac_h = `inch'/`incw'


su incwage_orig [fw=perwt] if coll
su incwage_orig [fw=perwt] if !coll


//bootstrap first
set seed 1234
qui{
forval i = 1/1000{
	
	if mod(`i', 20) == 0{
		noi di "Working on `i'"
	}
	
	use "$temp/acs_master_kid_sample", clear
	bsample 100000
	su move [fw=perwt] if racetype == 2
	local migb_boot = `r(mean)'
	su move [fw=perwt] if racetype == 3
	local migh_boot = `r(mean)'
	
	su incwage_orig [fw=perwt] if racetype == 2
	local incb_boot = `r(mean)'
	su incwage_orig [fw=perwt] if racetype == 3
	local inch_boot = `r(mean)'
	
	su incwage_orig [fw=perwt] if racetype == 1
	local incw_boot = `r(mean)'

	local frac_b_boot = `incb_boot'/`incw_boot'
	local frac_h_boot = `inch_boot'/`incw_boot'

		
	clear
	set obs 4
	gen moment = .
	replace moment = `migb_boot' in 1
	replace moment = `migh_boot' in 2
	replace moment = `frac_b_boot' in 3
	replace moment = `frac_h_boot' in 4
	xpose, clear
	save "$temp/boot/mig_moments_race_boot_`i'", replace		
}
}

clear
forval i = 1/1000{
    append using "$temp/boot/mig_moments_race_boot_`i'"
}
export delimited "$dir/Model/boot/mig_race_boot_sample.csv", novarn replace

clear
set obs 10
gen v1 = .
replace v1 = `collb'  in 1
replace v1 = `collh'  in 2
replace v1 = `coll_region_1' in 3
replace v1 = `coll_region_2' in 4
replace v1 = `coll_region_3' in 5
replace v1 = `coll_region_4' in 6
replace v1 = `migb'   in 7
replace v1 = `migh'   in 8
replace v1 = `frac_b'  in 9
replace v1 = `frac_h'   in 10
save "$temp/moments_race", replace

export delimited "$dir/Model/moments/moments_race.csv", novarn replace

//end of dofile